<?php
#注册插件
RegisterPlugin("os_preview","ActivePlugin_os_preview");
// 挂接口
function ActivePlugin_os_preview() {
    Add_Filter_Plugin('Filter_Plugin_Edit_Response3','os_preview_Edit_Response3'); // 挂载文章编辑页3号接口，放置预览按钮
    Add_Filter_Plugin('Filter_Plugin_Index_Begin','os_preview_Index_Begin'); // 挂载Index开始访问接口，获取预览命令
}

function os_preview_Edit_Response3() {
    global $zbp,$article;
    echo '<button id="os_preview" style="height:36px;line-height:36px;padding:0 25px;font-size:16px;border:none;color:#fff;background:#3a6ea5;display:inline-block;cursor:pointer;">预览文章</button><br>';
    echo '
        <script>
            var $previewForm = $("form");
            var preview = document.getElementById("os_preview");
            $(\'input[type="submit"]\').on("click",function(){
                $previewForm.attr({
                    "target":"_self"
                });
            });
            $(preview).on("click",function(e){
                $previewForm.attr({
                    "action":"'.$zbp->host.'?preview=article",
                    "target":"_blank"
                });
            });
        </script>
    ';
}

function os_preview_Index_Begin() {
    global $zbp;
    if (GetVars('preview','GET')=='article'&&$zbp->user->ID>0&&count($_POST)>0) {
        $article = new Post;
        $article->Title = '[预览]'.GetVars('Title','POST');
        $article->Content = GetVars('Content','POST');
        $article->Template = GetVars('Template','POST');
        $article->Time = GetVars('PostTime','POST');
        $article->CateID = GetVars('CateID','POST');
        $article->IsLock = true;
        $article->Type = 'article';
        $article->Author = $zbp->user;
        $zbp->template->SetTags('title',$article->Title);
    	$zbp->template->SetTags('article', $article);
    	$zbp->template->SetTags('type', 'article');
    	$zbp->template->SetTags('page', 1);
    	$zbp->template->SetTags('pagebar', null);
    	$zbp->template->SetTags('comments', null);

        $templates = (isset($zbp->templates))?$zbp->templates:$zbp->template->templates;
        if (isset($templates[$article->Template])) {
    		$zbp->template->SetTemplate($article->Template);
    	} else {
    		$zbp->template->SetTemplate('single');
    	}
        $zbp->template->Display();
        die();
    }
}

function InstallPlugin_os_preview() {}
function UninstallPlugin_os_preview() {}
